package app.productmanagement.web.Product.Service;

import app.productmanagement.Pojo.Result;
import app.productmanagement.web.Product.Pojo.Product;
import app.productmanagement.web.Product.Pojo.dto.ProductRequest;
import app.productmanagement.web.Product.Pojo.vo.ProductDetailVo;
import app.productmanagement.web.Product.Pojo.vo.ProductLimitVo;
import app.productmanagement.web.Product.Pojo.vo.ProductVo;
import app.productmanagement.web.Product.Service.impl.ProductServiceInterface;
import app.productmanagement.web.Product.copyMapper.copyProduct;
import app.productmanagement.web.Product.mapper.ProductMapper;
import app.productmanagement.web.ProductAndTag.Pojo.ProductTagMultipleTables;
import app.productmanagement.web.ProductAndTag.Service.ProductTagService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
@Slf4j
public class ProductService extends ServiceImpl<ProductMapper, Product> implements ProductServiceInterface {

    // 商品Mapper
    private final ProductMapper productMapper;
    // 商品的标签表Service
    private final ProductTagService productTagService;

    // POJO 复制转换器
    private final copyProduct cp;

    //链表查询
    public List<ProductTagMultipleTables> getWithTagIdAndProductInnerJoinTag(int tagId) {
        return productMapper.SelectWithTagIdAndProductInnerJoinTag(tagId);
    }

    public List<ProductTagMultipleTables> getWithProductIdAndProductInnerJoinTag(int productId) {
        return productMapper.SelectWithProductIdAndProductInnerJoinTag(productId);
    }

    // 商品Vo转换器
    private ProductVo getProductVo(Product product) {
        // 复制转换
        ProductVo productVo = cp.copyProductToProductVo(product);
        // 获取商品的标签
        productVo.setTags(this.getWithProductIdAndProductInnerJoinTag(product.getId()));
        return productVo;
    }

    // 插入商品
    @Override
    public Boolean insertProduct(Product product) {
        // 设置默认分类
        product.setCategoryId(product.getCategoryId() == null || product.getCategoryId() == 0 ? 1 : product.getCategoryId());
        //商品类别,商品价格和商品库存(不可以为负数)
        if (product.getPrice() <= 0 || product.getCategoryId() < 0 || product.getStock() < 0) {
            return false;
        }

        return productMapper.insert(product) > 0;
    }


    /***
     * 添加商品和商品标签
     */
    @Override
    public Result<ProductVo> addProductAndTag(ProductRequest productRequest) {


        Product product = cp.copyProductRequestToProduct(productRequest);

        if (!insertProduct(product)) {
            return Result.failure("添加商品失败");
        }
        // 保存商品标签
        productTagService.addProductTags(cp.getTagsFromProductRequestAndSetProductId(product.getId(), productRequest));
        return Result.success(getProductVo(product));
    }


    // 删除商品
    @Override
    public Result<Boolean> deleteProduct(Integer id) {

        if (productMapper.deleteById(id) > 0 & productTagService.deleteProductTags(true, id)) {
            return Result.success(true);
        } else {
            return Result.failure("删除商品失败，商品可能不存在");
        }
    }

    // 查询全部商品
    @Override
    public Result<ProductLimitVo> getAllProducts(int page, int limit) {

        // 第page 页，每页limit 条数据。
        Page<Product> page1 = new Page<>(page, limit);
        Page<Product> productPage = productMapper.selectPage(page1, null);

        List<ProductVo> productVos = new ArrayList<>();

        // 遍历查询到的商品，获取商品的标签
        productPage.getRecords().forEach(product -> {
            productVos.add(getProductVo(product));
        });

        // 返回分页结果
        return Result.success(new ProductLimitVo(productVos, productPage.getPages(), productPage.getCurrent(), productPage.getSize()));
    }

    // 获取单个商品
    @Override
    public Result<ProductDetailVo> getProduct(Integer id) {
        LambdaQueryWrapper<Product> lambdaQueryWrapper = new LambdaQueryWrapper<>(Product.class).eq(Product::getId, id);
        List<Product> products = productMapper.selectList(lambdaQueryWrapper);

        if (products.isEmpty()) {
            return Result.failure("没有这个商品");
        }
        Product product = products.get(products.size() - 1);
        Result.success(getProductVo(product));
        // 复制转换
        return null;
    }

    // 修改商品信息
    @Override
    public Result<Product> updateProduct(int id, ProductRequest productRequest) {
        Product product = cp.copyProductRequestToProduct(productRequest);
        product.setId(id);

        if (productMapper.updateById(product) > 0) {
            return Result.success(product);
        } else {
            return Result.failure("修改商品失败，商品可能不存在");
        }
    }
}
